/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018-12-16 Time: 23:06
 *
 * 检测权限
 */

import {Alert, PermissionsAndroid} from 'react-native';
import Permissions from 'react-native-permissions';

export const PermissionType = {
  Photos: 'photo', // 相册
  Camera: 'camera', // 相机
  Location: 'location', // 定位
  Microphone: 'microphone', // 麦克风
  Contacts: 'contacts' // 通讯录
};


class PermissionUtil {

  // 构造
  constructor(props) {

  }

  checkPermission = (success, fail, permission = []) => {
    let self = this;
    let flag = true;
    let per = [];
    if (permission.length <= 0) {
      return;
    }
    Permissions.checkMultiple(permission).then(status => {
      //直接修改设置 检测不到授权结果
      console.log('Permission status', status);
      Object.keys(status).map((k, v) => {
        if (status[k] === "authorized") {
        } else {
          per.push(k);
          flag = false;
        }
      });
      if (flag) {
        success();
      } else {
        if (per.length > 0) {
          self.request(success, fail, per, 0);
        }
      }
    })
  };

  request = (success, fail, per, i) => {
    if (i < per.length) {
      console.log('per', per);
      this.requestPermission(success, fail, per, i);
    } else {
      success();
    }
  };

  requestPermission = (success, fail, per, i) => {
    let self = this;
    console.log('per[i]', per[i]);
    Permissions.request(per[i]).then(res => {
      console.log('res', res);
      if (res !== 'authorized') {
        // if (fail) {
        //   fail();
        //   return;
        // }
        switch (per[i]) {
          case PermissionType.Camera:
            Alert.alert(
              '房务管家需要使用摄像头拍摄照片',
              "请允许房务管家使用您的相机.",
              [
                {text: '取消', style: 'cancel'},
                {text: '设置权限', onPress: Permissions.openSettings},
              ]
            );
            break;
          case PermissionType.Photos:
            Alert.alert(
              '房务管家需要使用相册加载证件',
              "请允许房务管家使用您的相册",
              [
                {text: '取消', style: 'cancel'},
                {text: '设置权限', onPress: Permissions.openSettings},
              ]
            );
            break;
          case PermissionType.Location:
            Alert.alert(
              '房务管家需要使用定位服务设置地址',
              "请允许房务管家使用您的位置服务",
              [
                {text: '取消', style: 'cancel'},
                {text: '设置权限', onPress: Permissions.openSettings},
              ]
            );
            break;
          case PermissionType.Microphone:
            Alert.alert(
              '提示',
              "请授予应用录音权限",
              [
                {text: '取消', style: 'cancel'},
                {text: '设置权限', onPress: Permissions.openSettings},
              ]
            );
            break;
          case PermissionType.Contacts:
            Alert.alert(
              '提示',
              "请授予应用获取联系人权限",
              [
                {text: '取消', style: 'cancel'},
                {text: '设置权限', onPress: Permissions.openSettings},
              ]
            );
            break;
          default:
            Alert.alert(
              '提示',
              "请授予应用权限",
              [
                {text: '取消', style: 'cancel'},
                {text: '设置权限', onPress: Permissions.openSettings},
              ]
            );
            break;
        }
      } else {
        self.request(success, fail, per, ++i);
      }
    }).catch(e => console.log(e))
  };
}

export default new PermissionUtil();